package com.k.base.jdk.fanxing;

import com.k.base.jdk.fanxing.domain.Apple;
import com.k.base.jdk.fanxing.domain.Banana;
import com.k.base.jdk.fanxing.domain.Fruit;

import java.util.*;

/**
 * @author k
 */
public class FanxingTest {
    private static void printList1(List<Object> list) {
        for (Object elem : list){
            System.out.println(elem + " ");
        }
    }
    private static <T> void printList2(List<T> list) {
        for (T elem : list){
            System.out.println(elem + " ");
        }
    }
    private static void printList3(List<?> list) {
        for (int i = 0;i<list.size();i++){
            System.out.println(list.get(i) + " ");
        }
    }
    public void testList(){
        List<Integer> test1 = Arrays.asList(1, 2, 3);
        List<String> test2 = Arrays.asList("one", "two", "three");
        List<Object> test3 = Arrays.asList(1, "two", 1.23);
        List<Fruit> test4 = Arrays.asList(new Apple(), new Banana());
//        printList1(test1);// err
//        printList1(test2); //err
//        printList1(test3); //ok
//        printList1(test4); //err

//        printList2(test1);// ok
//        printList2(test2); //ok
//        printList2(test3); //ok
//        printList2(test4); //ok

//        printList3(test1);// ok
//        printList3(test2); //ok
//        printList3(test3); //ok
//        printList3(test4); //ok
    }

    // Map<String, ?>只能是只读模式，不能增加，因为增加的时候不知道该写入什么类型的值
    //    io.jsonwebtoken 中有类似
    //     JwtBuilder setClaims(Map<String, ?> var1);
    //    JwtBuilder addClaims(Map<String, Object> var1);
    private static void map1(Map<String,?> map){
        System.out.println(map);
    }
    // Map<String, Object>可以读和写，只要是所有Object类的子类都可以。
    private static void map2(Map<String,Object> map){
        System.out.println(map);
    }
    private static <T> void map3(Map<String,T> map){
        System.out.println(map);
    }
    public static void testMap(){
        Map<String,String> mapStrStr = new HashMap<>(); mapStrStr.put("a1","v1");
        Map<String,Integer> mapStrInt = new HashMap<>(); mapStrInt.put("a2",2);
        Map<String,Object> mapStrObj = new HashMap<>(); mapStrObj.put("a3",3);
        Map<String,Fruit> mapStrBean = new HashMap<>(); mapStrBean.put("a3",new Apple());
//        map1(mapStrStr);
//        map1(mapStrInt);
//        map1(mapStrObj);
//        map1(mapStrBean);
//        System.out.println("==========");

//        map2(mapStrStr);// err
//        map2(mapStrInt);// err
        map2(mapStrObj);// ok
//        map2(mapStrBean); // err
        System.out.println("==========");
    }

    public void testFanxing(String[] args){
        Fanxing<Double> d = new Fanxing<>();
        d.method(3.4);
        Boolean b = Fanxing.staticMethod(args.length > 0);
        System.out.println(b);
        d.show("haha");
        d.print(4);
    }

    public static void main(String[] args) {
//        testMap();
        Map<String,Object> map1 = new HashMap<>();
        Map<String,Object> map2 = new HashMap<>();
        map1.put("mapa1","a1");
        map1.put("key","key1");
        map2.put("mapa2","a2");
        map2.put("key","key2");
        map2.putAll(map1);
        System.out.println(map2);
    }
}
